package com.gzdh.dishrecognition.ui.fragment.sysset;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.text.InputType;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.config.SysConstConfig;
import com.gzdh.dishrecognition.event.CartEvent;
import com.gzdh.dishrecognition.ui.dialog.InputDialog;
import com.gzdh.dishrecognition.ui.view.OptionItemView;
import com.gzdh.dishrecognition.util.SystemUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.LinkedHashMap;
import java.util.Map;

public class FreeModeSetFragment extends Fragment {
    private Context mContext;

    private Activity activity;

    public FreeModeSetFragment(Activity activity) {
        this.activity = activity;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        mContext = inflater.getContext();
        View view = getViews();

        return view;
    }

    private View getViews() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        LinearLayout mainLayout = new LinearLayout(mContext);
        mainLayout.setLayoutParams(layoutParams);
        mainLayout.setBackgroundColor(Color.WHITE);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        mainLayout.addView(getTitleView());   //标题初始化
        mainLayout.addView(getBody());        //主题内容
        return mainLayout;
    }


    private View getTitleView() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout titleLinear = new LinearLayout(mContext);
        titleLinear.setLayoutParams(layoutParams);
        titleLinear.setBackgroundColor(Color.WHITE);
        titleLinear.setOrientation(LinearLayout.VERTICAL);

        RelativeLayout titleRv = new RelativeLayout(mContext);
        titleRv.setLayoutParams(layoutParams);
        titleLinear.addView(titleRv);

        //-------------------------标题
        TextView titleTv = new TextView(mContext);
        titleTv.setText(" 自 由 模 式 基 础 设 置 ");
        RelativeLayout.LayoutParams tt1 = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        int tt1Margins = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, this.getResources().getDisplayMetrics());
        tt1.setMargins(tt1Margins, tt1Margins, tt1Margins, tt1Margins);
        tt1.addRule(RelativeLayout.CENTER_IN_PARENT);
        titleTv.setLayoutParams(tt1);
        titleTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        titleRv.addView(titleTv);

        //----------------------------下划线
        View view = new View(mContext);
        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        view.setBackgroundColor(Color.rgb(224, 224, 224));
        titleLinear.addView(view);

        return titleLinear;
    }

    private View getBody() {
        ScrollView scrollView = new ScrollView(mContext);
        scrollView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        StateListDrawable selector = new StateListDrawable();

        selector.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.color.white));
        selector.addState(new int[]{}, getResources().getDrawable(R.color.white));


        LinearLayout mainLinear = new LinearLayout(mContext);
        mainLinear.setOrientation(LinearLayout.VERTICAL);
        scrollView.addView(mainLinear);

        OptionItemView textSizeView = new OptionItemView(mContext);
        int heightInPx = (int)(45 * getResources().getDisplayMetrics().density);
        LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, heightInPx);
        textSizeView.setLayoutParams(linearParams);
        textSizeView.setPadding(20, 20, 20, 20);
        textSizeView.setBackground(selector);
        textSizeView.setLeftImageMarginLeft(20);
        textSizeView.setLeftImageMarginRight(18);
        textSizeView.setLeftImage(drawableToBitmap(getResources().getDrawable(R.mipmap.setting_icon)));
        textSizeView.setLeftText("键盘输入消费金额字体大小");
        textSizeView.setLeftTextSize(16);
        textSizeView.setLeftTextColor(Color.BLACK);
        textSizeView.setRightImage(drawableToBitmap(getResources().getDrawable(R.mipmap.enter_icon)));
        textSizeView.setRightText(String.valueOf(SysConstConfig.getInstance().getFreeInputAmountFontSize()));
        textSizeView.setOnClickListener(view -> {
            InputDialog inputDialog = new InputDialog(mContext);
            inputDialog.show();
            inputDialog.getMsgTv().setText("请输入键盘输入消费金额字体大小");
            inputDialog.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
            inputDialog.getEditText().setText(String.valueOf(SysConstConfig.getInstance().getFreeInputAmountFontSize()));
            inputDialog.getOkTv().setOnClickListener(view1 -> {
                String row = inputDialog.getEditText().getText().toString();
                if (!"".equals(row)) {
                    int rowInt = Integer.parseInt(row);
                    SysConstConfig.getInstance().setFreeInputAmountFontSize(rowInt);
                    inputDialog.dismiss();
                    textSizeView.setRightText(String.valueOf(SysConstConfig.getInstance().getFreeInputAmountFontSize()));
                    EventBus.getDefault().post(new CartEvent());
                } else {
                    Toast.makeText(mContext, "输入有误，请重新输入", Toast.LENGTH_LONG).show();
                }
            });
        });
        mainLinear.addView(textSizeView);

        View line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        mainLinear.addView(line);

        OptionItemView freeTotalAmountFontSizeView = new OptionItemView(mContext);
        freeTotalAmountFontSizeView.setPadding(20, 20, 20, 20);
        freeTotalAmountFontSizeView.setLayoutParams(linearParams);
        selector = new StateListDrawable();
        selector.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.color.white));
        selector.addState(new int[]{}, getResources().getDrawable(R.color.white));
        freeTotalAmountFontSizeView.setBackground(selector);
        freeTotalAmountFontSizeView.setLeftImageMarginLeft(20);
        freeTotalAmountFontSizeView.setLeftImageMarginRight(18);
        freeTotalAmountFontSizeView.setLeftImage(drawableToBitmap(getResources().getDrawable(R.mipmap.setting_icon)));
        freeTotalAmountFontSizeView.setLeftText("键盘输入消费合计金额字体大小");
        freeTotalAmountFontSizeView.setLeftTextSize(16);
        freeTotalAmountFontSizeView.setLeftTextColor(Color.BLACK);
        freeTotalAmountFontSizeView.setRightText(String.valueOf(SysConstConfig.getInstance().getFreeTotalAmountFontSize()));
        freeTotalAmountFontSizeView.setRightImage(drawableToBitmap(getResources().getDrawable(R.mipmap.enter_icon)));
        freeTotalAmountFontSizeView.setOnClickListener(view -> {
            InputDialog inputDialog = new InputDialog(mContext);
            inputDialog.show();
            inputDialog.getMsgTv().setText("请输入消费合计金额字体大小");
            inputDialog.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
            inputDialog.getEditText().setText(String.valueOf(SysConstConfig.getInstance().getFreeTotalAmountFontSize()));
            inputDialog.getOkTv().setOnClickListener(view1 -> {
                String row = inputDialog.getEditText().getText().toString();
                if (!"".equals(row)) {
                    int rowInt = Integer.parseInt(row);
                    SysConstConfig.getInstance().setFreeTotalAmountFontSize(rowInt);
                    inputDialog.dismiss();
                    freeTotalAmountFontSizeView.setRightText(String.valueOf(SysConstConfig.getInstance().getFreeTotalAmountFontSize()));
                    EventBus.getDefault().post(new CartEvent());
                } else {
                    Toast.makeText(mContext, "输入有误，请重新输入", Toast.LENGTH_LONG).show();
                }
            });
        });
        mainLinear.addView(freeTotalAmountFontSizeView);


        line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        mainLinear.addView(line);

        OptionItemView isShowRightKeyboardView = new OptionItemView(mContext);
        isShowRightKeyboardView.setLayoutParams(linearParams);
        isShowRightKeyboardView.setPadding(20, 20, 20, 20);
        selector = new StateListDrawable();
        selector.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.color.white));
        selector.addState(new int[]{}, getResources().getDrawable(R.color.white));
        isShowRightKeyboardView.setBackground(selector);
        isShowRightKeyboardView.setLeftImageMarginLeft(20);
        isShowRightKeyboardView.setLeftImageMarginRight(18);
        isShowRightKeyboardView.setLeftImage(drawableToBitmap(getResources().getDrawable(R.mipmap.setting_icon)));
        isShowRightKeyboardView.setLeftText("是否显示右边数字键盘模块");
        isShowRightKeyboardView.setLeftTextSize(16);
        isShowRightKeyboardView.setLeftTextColor(Color.BLACK);
        isShowRightKeyboardView.setRightImage(drawableToBitmap(getResources().getDrawable(R.mipmap.enter_icon)));
        isShowRightKeyboardView.setRightText(SysConstConfig.getInstance().isShowRightKeyboard() ? "开启" : "关闭");
        isShowRightKeyboardView.setOnClickListener(view -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("是否显示右边数字键盘模块");
            final Map<String, Boolean> map = new LinkedHashMap<>();
            map.put("开启", true);
            map.put("关闭", false);
            final String[] keysTemp = new String[2];
            final String[] keys = map.keySet().toArray(keysTemp);
            builder.setItems(keys, (dialog, which) -> {
                boolean state = map.get(keys[which]);
                SysConstConfig.getInstance().setShowRightKeyboard(state);
                isShowRightKeyboardView.setRightText(SysConstConfig.getInstance().isShowRightKeyboard() ? "开启" : "关闭");
                EventBus.getDefault().post(new CartEvent());
            });
            builder.show();
        });
        mainLinear.addView(isShowRightKeyboardView);

        line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        mainLinear.addView(line);

        OptionItemView rightKeyboardDisplaySizeView = new OptionItemView(mContext);
        rightKeyboardDisplaySizeView.setLayoutParams(linearParams);
        rightKeyboardDisplaySizeView.setPadding(20, 20, 20, 20);
        selector = new StateListDrawable();
        selector.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.color.white));
        selector.addState(new int[]{}, getResources().getDrawable(R.color.white));
        rightKeyboardDisplaySizeView.setBackground(selector);
        rightKeyboardDisplaySizeView.setLeftImageMarginLeft(20);
        rightKeyboardDisplaySizeView.setLeftImageMarginRight(18);
        rightKeyboardDisplaySizeView.setLeftImage(drawableToBitmap(getResources().getDrawable(R.mipmap.setting_icon)));
        rightKeyboardDisplaySizeView.setLeftText("数字键盘模块显示规格");
        rightKeyboardDisplaySizeView.setLeftTextSize(16);
        rightKeyboardDisplaySizeView.setLeftTextColor(Color.BLACK);
        rightKeyboardDisplaySizeView.setRightImage(drawableToBitmap(getResources().getDrawable(R.mipmap.enter_icon)));
        rightKeyboardDisplaySizeView.setRightText(SystemUtils.getDisplaySize(SysConstConfig.getInstance().getRightKeyboardDisplaySize()));
        rightKeyboardDisplaySizeView.setOnClickListener(view -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("数字键盘模块显示规格大小");
            final Map<String, Integer> map = new LinkedHashMap<>();
            map.put("大", 2);
            map.put("中", 1);
            map.put("小", 0);
            final String[] keysTemp = new String[3];
            final String[] keys = map.keySet().toArray(keysTemp);
            builder.setItems(keys, (dialog, which) -> {
                int state = map.get(keys[which]);
                SysConstConfig.getInstance().setRightKeyboardDisplaySize(state);
                rightKeyboardDisplaySizeView.setRightText(SystemUtils.getDisplaySize(SysConstConfig.getInstance().getRightKeyboardDisplaySize()));
                EventBus.getDefault().post(new CartEvent());
            });
            builder.show();
        });
        mainLinear.addView(rightKeyboardDisplaySizeView);

        line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        mainLinear.addView(line);

        OptionItemView BottomButtonSizeView = new OptionItemView(mContext);
        BottomButtonSizeView.setLayoutParams(linearParams);
        BottomButtonSizeView.setPadding(20, 20, 20, 20);
        selector = new StateListDrawable();
        selector.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(R.color.gray8));
        selector.addState(new int[]{android.R.attr.state_selected}, getResources().getDrawable(R.color.white));
        selector.addState(new int[]{}, getResources().getDrawable(R.color.white));
        BottomButtonSizeView.setBackground(selector);
        BottomButtonSizeView.setLeftImageMarginLeft(20);
        BottomButtonSizeView.setLeftImageMarginRight(18);
        BottomButtonSizeView.setLeftImage(drawableToBitmap(getResources().getDrawable(R.mipmap.setting_icon)));
        BottomButtonSizeView.setLeftText("底部按钮显示大小");
        BottomButtonSizeView.setLeftTextSize(16);
        BottomButtonSizeView.setLeftTextColor(Color.BLACK);
        BottomButtonSizeView.setRightImage(drawableToBitmap(getResources().getDrawable(R.mipmap.enter_icon)));
        BottomButtonSizeView.setRightText(SystemUtils.getDisplaySize(SysConstConfig.getInstance().getInputNumberBottomButtonSize()));
        BottomButtonSizeView.setOnClickListener(view -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("底部按钮显示大小");
            final Map<String, Integer> map = new LinkedHashMap<>();
            map.put("大", 2);
            map.put("中", 1);
            map.put("小", 0);
            final String[] keysTemp = new String[3];
            final String[] keys = map.keySet().toArray(keysTemp);
            builder.setItems(keys, (dialog, which) -> {
                int state = map.get(keys[which]);
                SysConstConfig.getInstance().setInputNumberBottomButtonSize(state);
                BottomButtonSizeView.setRightText(SystemUtils.getDisplaySize(SysConstConfig.getInstance().getInputNumberBottomButtonSize()));
                EventBus.getDefault().post(new CartEvent());
            });
            builder.show();
        });
        mainLinear.addView(BottomButtonSizeView);

        line = new View(mContext);
        line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1));
        line.setBackgroundColor(Color.rgb(224, 224, 224));
        mainLinear.addView(line);

        return scrollView;
    }


    public static Bitmap drawableToBitmap(Drawable drawable) {
        // 取 drawable 的长宽
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();

        // 取 drawable 的颜色格式
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        // 建立对应 bitmap
        Bitmap bitmap = Bitmap.createBitmap(w, h, config);
        // 建立对应 bitmap 的画布
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        // 把 drawable 内容画到画布中
        drawable.draw(canvas);
        return bitmap;
    }


}
